﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SubSpec;
using PMKB_Repository;
using KVP;
using Xunit;
using System.IO;
using PMKB_DAL;
using Xunit.Extensions;
using System.Threading;
using FastDatabaseQuery;
using common;
using System.Configuration;

namespace PMKB_Testing
{
    
    public class clsAddTagsAndLinks
    {
        static bool IsAppHB=false;
        static readonly string Connection ;
        static clsAddTagsAndLinks()
        {
            //error at appHB

            //first, test if it is an sql server defined
            string s = "SQLSERVER_CONNECTION_STRING";
            string val = ConfigurationManager.AppSettings[s];
            if ((!string.IsNullOrEmpty(val)) && (val.ToLower() != "appharbor"))
            {
                Connection = val;
                IsAppHB = true;
            }
            else
            {
                //if not sql server, default to connection
                Connection = "name=PMKB";
            }
            //if(File.Exists("PMKB.sdf"))
            //{ 
            //    File.Delete("PMKB.sdf");
            //};
            StartTestWithEmptyDatabase();           
           
        }
        static void StartTestWithEmptyDatabase()
        {
            FastDatabaseQuery.DBInitializer.Strategy = eDatabaseStrategy.DropCreateDatabaseAlways;
            //HelperTables.FastDeleteAllTables(Connection);
            //if (File.Exists("PMKB.sdf"))
            {
                IFastDeleteAll lt = new Link_Tag_List(Connection);
                lt.FastDeleteAll();

                lt = new Link_List(Connection);
                lt.FastDeleteAll();

                lt = new Tag_List(Connection);
                lt.FastDeleteAll();

                lt = new Link_OLAP_List(Connection);
                lt.FastDeleteAll();


            }

        }

        [Specification]
        [Trace]
        public void AddLink()
        {
            if (IsAppHB)
                return;
            StartTestWithEmptyDatabase(); 
            I_Link link=null;
            I_Link_OLAP ol = null;

            "When inserting tags ".Context(() => { 
                 InitialData.InsertTags(Connection); 
            });
            "inserting new link".Do(() => {

                PMKB_Repository.clsLoadTags l = new PMKB_Repository.clsLoadTags();
                l.ConnectionName = Connection;

                LinkAddOrEdit a = new LinkAddOrEdit();
                a.ConnectionName = Connection;

                a.IDSelectedTags = l.AllTags().Select(i => i.Key).ToList();
                a.link = new PMKB_DAL.Link();
                a.link.LinkName = "andrei";
                a.link.LinkUrl = "asda";
                a.link.LinkDescription = " do not know";
                a.SaveNew();
                var id = a.link.LinkID;
                a = new LinkAddOrEdit();
                a.ConnectionName = Connection;
                link = a.LoadLink(id);
                var ll = new clsLoadLinks();
                ll.ConnectionName = Connection;
                ll.LoadLinks(1, 100, "", true);
                ol = ll.links.Where(item => item.LinkID == link.LinkID).FirstOrDefault();

                
                    
            });
            "it does exists link".Assert(() => Assert.NotNull(link));

            "it does exists link in olap".Assert(() =>
                {
                    var ll = new clsLoadLinks();
                    ll.ConnectionName = Connection;
                    ll.LoadLinks(1, 10, null, true);
                    var olap = ll.links.Where(item => item.LinkID == link.LinkID).FirstOrDefault();
                    Assert.NotNull(olap);
                    Assert.Equal<I_Link>((olap as Link_OLAP), link, new EqualityLambda<I_Link>(null,(a, b) => Link.ArePropertiesEqualDynamic(a, b, true)));

                }
                );


            
            
        }
        //[Specification]
        //[Trace]
        //public void AddAndDeleteLink()
        //{
        //    StartTestWithEmptyDatabase();


        //}
        [Specification]
        [Trace]
        public void AddTags()
        {
            if (IsAppHB)
                return;
            StartTestWithEmptyDatabase(); 
            clsLoadTags l = new clsLoadTags();
            

             List<KVPNew<long, string>> tags=null;
             "Given a new database and loading tags ".Context(() => { l.ConnectionName = Connection;  tags = l.AllTags(); });
             "it does not have data".Assert(() => Assert.Equal(0,tags.Count));
             "inserting default data".Do(() => { 
                 InitialData.InsertTags(Connection);
                 l = new clsLoadTags();
                 l.ConnectionName = Connection;
             });
             "it does not have data".Assert(() => Assert.True(l.AllTags().Count > 0));
        }

        [Specification]
        [Trace]
        public void RenameTag()
        {
            if (IsAppHB)
                return;
            long idTag = 0;
            long idLink = 0;
            string oldName="", newName = Guid.NewGuid().ToString();
            StartTestWithEmptyDatabase(); 
            "Given a new database and inserting tags and a link".Context(() => {
                idTag = InitialData.InsertTags(Connection).First();
                LinkAddOrEdit add = new LinkAddOrEdit();
                add.ConnectionName = Connection;
                add.IDSelectedTags = new List<long>() { idTag };
                add.link = new Link() { LinkName = "aa", LinkUrl = "a", LinkDescription = "b" };
                add.SaveNew();
                idLink = add.link.LinkID;

            });
            "change the tag name".Do(() =>
            {
                clsLoadTags c = new clsLoadTags();
                c.ConnectionName = Connection;
                var dt = c.LoadTag(idTag, 1, 10);
                oldName = dt.tag.TagName;             
                c.RenameTags(idTag, newName);


            });
            "remains same name as tag".Assert(() => {
                 clsLoadTags c=new clsLoadTags();
                    c.ConnectionName=Connection;
                var dt=c.LoadTag(idTag,1,10);
                Assert.Equal(dt.tag.TagName,newName);
            });
            //"remains same name in olap".Assert(() =>
            //{
            //    clsLoadLinks load = new clsLoadLinks();
            //    load.ConnectionName = Connection;
            //    load.LoadLinks(1, 10, "", true);
            //    var link = load.links.FirstOrDefault(item => item.LinkID == idLink);
            //    Assert.NotNull(link);
                //var tags = PMKB_Repository.clsLoadLinks.tags(link);
                //Console.Write("tags:" + tags.Count());
                //Assert.Null(tags.FirstOrDefault(item => item == oldName));
                //Assert.NotNull(tags.FirstOrDefault(item => item == newName));

            //});
                
            
        }
       
    }
}
